#Replication Code for 
#Digital intermediaries, market competition, and citizen-state interactions
#Authors- Anustubh Agnihotri
#The Files replicates the main results linking number of digital intermediaries with
#responsiveness 

rm(list = ls())

pks <- c("tidyverse", "haven", "lfe", "stargazer", 
         "xtable", "kableExtra","ggplot2","dplyr","lubridate","zoo",
         "astsa","panelView")
lapply(pks, function(x) if (!require(x, character.only=T)){library(x, character.only = T)})

##Enter the local directory where the files are stored
dir=''

##load the datasets
load(paste0(dir,'Intermediaries_Over_Time.RData'))
load(paste0(dir,'Main_Data.RData'))


###The first step merges the dataset1 which has state responsiveness
###with the dataset with number of Digital Intermediaries Per Year Month

main_dataset_replication$tuid_csc_year_month<-paste0(main_dataset_replication$ID,
                                         main_dataset_replication$Year_Month_Applied)

#Create the same merging variable in the other dataser
Intermediary_Monthwise_Replication$tuid_csc_year_month<-paste0(Intermediary_Monthwise_Replication$ID,
                                                               Intermediary_Monthwise_Replication$Year_Month)

#Check for missing merging variable variables
table(main_dataset_replication$tuid_csc_year_month%in%Intermediary_Monthwise_Replication$tuid_csc_year_month)

#merge
main_dataset_replication<-dplyr::left_join(main_dataset_replication,dplyr::select(Intermediary_Monthwise_Replication,!ID),by='tuid_csc_year_month')

#create a variable for Year-Qtr
main_dataset_replication$Year_Qtr_Applied<-as.yearqtr(main_dataset_replication$Year_Month_Applied)

#Create volume variable
main_dataset_replication<-main_dataset_replication%>%
  group_by(Year_Month_Applied,ID)%>%
  mutate(All_Volume=n())


########Main Results Using TIme and Unit FIxed Effects
#The results below represent columns in Table A2 
##Column 1 Shows relationship between number of Digital Intermediaries and 
##resposivenss in Days
summary(felm(processing_time_n~No_Dig_Int|ID+Year_Qtr_Applied|0|CSCID,data = main_dataset_replication))
##Column 2 Shows relationship between number of Digital Intermediaries and 
##resposivenss in Days while controlling for Volume of Applications 
summary(felm(processing_time_n~No_Dig_Int+All_Volume|ID+Year_Qtr_Applied|0|CSCID,data = main_dataset_replication))
##Column 3 Shows relationship between number of Digital Intermediaries and 
##resposivenss in logged days
summary(felm(processing_time_log~No_Dig_Int|ID+Year_Qtr_Applied|0|CSCID,data = main_dataset_replication))
##Column 4 Shows relationship between number of Digital Intermediaries and 
##resposivenss in logged Days while Controlling for Volume of Applications
summary(felm(processing_time_log~No_Dig_Int+All_Volume|ID+Year_Qtr_Applied|0|CSCID,data = main_dataset_replication))


#Replicate the panel view figure A5
main_treatment_overtime<-main_dataset_replication%>%
  group_by(ID,Year_Month_Applied)%>%
  summarise(N=n(),
            VLEs=mean(No_Dig_Int),
            ptime=mean(processing_time_n,na.rm=T))
#
panelView(ptime~VLEs , data = main_treatment_overtime, index = c("ID","Year_Month_Applied"), 
          xlab = "Year Month", ylab = "Tehsil")+
  theme(axis.text.x = element_text(angle = 90, vjust = 0.5, hjust=1))
